<script setup>
	import tools from '../../../../utils/tools';
	import {
		getStockType,
		update
	} from '@/api/task.js';
	import {
		useAccountStore
	} from '@/store';
	import {getDict} from "@/api/login.js"
	const {
		proxy
	} = getCurrentInstance();
	const accountStore = useAccountStore();
	let warehouse = reactive(getWarehouse());
	console.log(warehouse,'=')
	const columns = ref([]);
	let fileList = ref([]);
	let date = ref("");
	let calendarsField = ref('');
	let loading = ref(false);
	let rules = {
		"thisGoDownNum": {
			type: 'string',
			required: true,
			message: '入库数量不能为空',
			trigger: ['blur', 'change']
		},
		"stockCost": {
			type: 'string',
			required: true,
			message: '综合成本单价不能为空',
			trigger: ['blur', 'change']
		},
		"manufactureDate": {
			type: 'string',
			required: true,
			message: '生产日期不能为空',
			trigger: ['blur', 'change']
		},
		"expiryDate": {
			type: 'string',
			required: true,
			message: '到期日期不能为空',
			trigger: ['blur', 'change']
		},
		"cwcSubStorageName":{
			type: 'string',
			required: true,
			message: '子库存类型不能为空',
			trigger: ['blur', 'change']
		}
	}
	let params = reactive({
		taskStatus: "",
		productListId: "",
		cwcId: "",
		id: "",
		// 入库凭证
		picUrl: "",
		// 入库说明
		description: "",
		// 本次入库数量
		thisGoDownNum: "",
		// 综合成本
		stockCost: "",
		// 生成日期 
		manufactureDate: "",
		// 到期日期
		expiryDate: "",
		// 子库存名称
		cwcSubStorageName: "",
		// 子库存id
		cwcSubStorageId: ""
	})
	let item;
	onLoad((option) => {
		if (option.item) {
			item = JSON.parse(decodeURIComponent(option.item));
			console.log(item,'item')
			params.id = item.id;
			params.productListId = item.productListId;
			// params.cwcId = item.cwcId;
			params.cwcId = item.cwcId;
			init();
		}
	})

	function pickerConfirm({
		value: [data = null]
	}) {
		if (data) {
			params.cwcSubStorageName = data.name;
			params.cwcSubStorageId = data?.id||"";
		}
	}

	function init() {
		getStockTypeList();
	}
	function findDict(dictType){
		return new Promise((re,rj)=>{
			getDict({dictType}).then(res=>{
				let {data:{list=[]}} = res;
				re(list) 
			})
		})
		
	}
	function getStockTypeList() {
		let {
			productListId,
			cwcId
		} = item;
		getStockType({
			productListId,
			cwcId
		}).then( async res => {
			console.log(res,'返回结果')  
			if(!res?.data?.list.length){ 
				let list =  await findDict('sub_storage_type'); 
				columns.value.push(list.map(i=> {
					return {
						name:i.label
					}
				} ).reverse() );
				 
				console.log(columns.value) 
			}else{
				columns.value.push(res?.data?.list || []); 
			}
		})
	}

	function openCalendars(e) {
		calendarsField.value = e;
		proxy.$refs.calendars.open();
	}

	function calendarsConfirm({
		fulldate
	}) {
		params[calendarsField.value] = fulldate;
	}

	function afterRead(event) {
		let filePath = event.file[0].url;
		tools.updateFile({
			filePath
		}).then(res => {
			fileList.value.push(res);
			params.picUrl = res.url;
		})
	}

	function deletePic(event) {
		fileList.value = [];
		params.picUrl = ""
	}
function getWarehouse() {
		let id =  uni.getStorageSync('Rel-Id');
		return (accountStore?.centralWarehouse?.userRelRespVOList || []).find(i => i.id ==id) || null;
	}
	async function submit(e) {   
		if (loading.value) return; 
		params.taskStatus = e;  
		await proxy.$refs.form.validate().then((x) => { 
			loading.value = true; 
			update(params).then(res => {
				if (res.code == 0) {
					uni.showToast({
						title: '操作成功',
						icon: 'none'
					});
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/task/index'
						})
					}, 1000)
				}
			}).catch(err => {});
		}).finally(() => {
			setTimeout(()=>{
				loading.value = false;
			},1000)
		});


	}
</script>
<template>
	<view class="container home-pd">
		<view class="card">
			<uv-form :model="params" :rules="rules" labelPosition="left" ref="form">
				<uv-form-item  @click="proxy.$refs.picker.open()" prop="cwcSubStorageName" required label-width="200rpx" label="子库存类型" borderBottom>
					<view v-if="params.cwcSubStorageName">{{params.cwcSubStorageName}}</view>
					<view v-else style="color: rgb(192, 196, 204);">
						请选择子库存类型
					</view>
				</uv-form-item>
				<uv-form-item prop="thisGoDownNum" required label-width="200rpx" label="入库数量" borderBottom>
					<uv-input v-model="params.thisGoDownNum" placeholder="请输入入库数量" border="none" />
				</uv-form-item>
				<uv-form-item prop="stockCost" required label-width="200rpx" label="综合成本单价" borderBottom>
					<uv-input v-model="params.stockCost" placeholder="请输入综合成本单价" border="none" />
				</uv-form-item>
				<uv-form-item label-width="200rpx" label="入库成本单价" borderBottom>
					12.00元
				</uv-form-item>
				<uv-form-item prop="manufactureDate" required label-width="200rpx" label="生产日期" borderBottom>
					<uv-input v-model="params.manufactureDate" @click="openCalendars('manufactureDate')"
						disabledColor="#ffffff" disabled suffixIcon="calendar" placeholder="请选择生产日期" border="none" />
				</uv-form-item>
				<uv-form-item prop="expiryDate" @click="openCalendars('expiryDate')" required label-width="200rpx"
					label="到期日期" borderBottom>
					<uv-input v-model="params.expiryDate" disabledColor="#ffffff" disabled suffixIcon="calendar"
						placeholder="请选择到期日期" border="none" />
				</uv-form-item>
				<uv-form-item prop="picUrl" class="column" label-width="200rpx" label="入库凭证" borderBottom>
					<uv-upload :fileList="fileList" multiple :maxCount="1" @afterRead="afterRead"
						@delete="deletePic"></uv-upload>
				</uv-form-item>
				<uv-form-item prop="description" label-width="200rpx" label="入库说明">
					<uv-input v-model="params.description" placeholder="请输入入库说明" border="none" />
				</uv-form-item>
			</uv-form>
		</view>
		<view class="button-group">
			<view :class="{loading:params.taskStatus=='0' && loading}" @click="submit('0')" class="bt-1 flex flex-justify-center flex-items-center">
				<uv-loading-icon color="#FFFFFF" v-if=" params.taskStatus=='0' && loading" />
				<view v-else>确认入库</view>
			</view>
			<view :class="{loading:params.taskStatus=='1' && loading}" @click="submit('1')" class="bt-2 flex flex-justify-center flex-items-center">
				<uv-loading-icon color="#000000" v-if="params.taskStatus=='1' && loading" />
				<view v-else>拒绝入库</view>
			</view>
		</view>
		<uv-calendars :date="params[calendarsField]" ref="calendars" @confirm="calendarsConfirm" />
		<uv-picker ref="picker" keyName="name" :columns="columns" @confirm="pickerConfirm"></uv-picker>
	</view>
</template>
<style lang="scss" scoped>
	:deep(.uv-form-item__body) {
		padding: 38rpx 0 !important;
	}

	.column {
		:deep(.uv-form-item__body) {
			flex-direction: column !important;
			padding: 24rpx 0 !important;
		}

		:deep(.uv-form-item__body__right) {
			margin-top: 24rpx;
		}
	}

	:deep(.uv-form-item__body__left__content__required) {
		right: 10rpx;
		left: inherit !important;
	}

	:deep(.uv-form-item__body__left__content__label) {
		font-weight: 400;
		font-size: 28rpx;
		color: #3C3F4D;
	}

	@import 'index.scss';
</style>